//
//  DcNAlbumPhotosCell.m
//  Decathlon
//
//  Created on 14-2-11.
//  Copyright (c) 2014年. All rights reserved.
//

#import "FTAlbumPhotosCell.h"
#import "AlbumDefine.h"
#import "RCSAlubmButton.h"

static const CGFloat kPhotoViewPaddingLeft  = 10.0f;
static const CGFloat kPhotoViewPaddingTop   = 10.0f;
static const CGFloat kPhotoWidth            = 68.f;
static const CGFloat kPhotoHeight           = 68.f;
static const CGFloat kSelectBtnWidth        = 34.0f;
static const CGFloat kSelectBtnHeiht        = 34.0f;

@implementation FTAlbumPhotosCell

@synthesize firstPhotoBtn  = _firstPhotoBtn;
@synthesize secondPhotoBtn = _secondPhotoBtn;
@synthesize thirdPhotoBtn  = _thirdPhotoBtn;
@synthesize fourPhotoBtn   = _fourPhotoBtn;
@synthesize delegate       = _delegate;



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSInteger kwidth = [UIScreen mainScreen].applicationFrame.size.width;
        CGFloat wSpace = (kwidth - kPhotoWidth * 4 - kPhotoViewPaddingLeft * 2) / 3;
        
        for (int i = 0; i < 4; i++)
        {
            RCSAlubmButton* PhotoBtn = [[RCSAlubmButton alloc] init];
            CGRect rect = CGRectMake(kPhotoViewPaddingLeft + wSpace * i + kPhotoWidth * i,
                                     kPhotoViewPaddingTop,
                                     kPhotoWidth,
                                     kPhotoHeight);
            PhotoBtn.frame = rect;
            PhotoBtn.layer.cornerRadius = 3;
            PhotoBtn.clipsToBounds = YES;
            PhotoBtn.backgroundColor = [UIColor clearColor];
            [self addSelectImageToButton:PhotoBtn];
            [PhotoBtn addTarget:self action:@selector(photoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            
            [self setPhotoButton:PhotoBtn buttonIndex:i];
            [self.contentView addSubview:PhotoBtn];
            
        }
    }
    return self;
}

- (void)dealloc
{
    self.firstPhotoBtn = nil;
    self.secondPhotoBtn = nil;
    self.thirdPhotoBtn = nil;
    self.fourPhotoBtn = nil;
    
}

-(void)setPhotoButton:(RCSAlubmButton*)button buttonIndex:(NSInteger)index
{
    if (index == 0)
    {
        _firstPhotoBtn = button;
    }
    else if (index == 1)
    {
        _secondPhotoBtn = button;
    }
    else if (index == 2)
    {
        _thirdPhotoBtn = button;
    }
    else{
        _fourPhotoBtn = button;
    }
}


/**
 *  选择按钮点击事件
 *
 */
-(void)selectPhotoBtnPressed:(UIButton*)button
{
    if (self.delegate &&[self.delegate respondsToSelector:@selector(selectPhotoBtnPressed:)])
    {
        [self.delegate selectPhotoBtnPressed:button];
    }
}

/**
 *  头像点击事件
 *
 */
-(void)photoButtonPressed:(id)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(photoBtnPressed:)])
    {
        [self.delegate photoBtnPressed:button];
    }
}

/**
 *  添加选择按钮
 *
 */
-(void)addSelectImageToButton:(RCSAlubmButton *)button
{
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.frame = CGRectMake(kSelectBtnWidth, 0, kSelectBtnWidth, kSelectBtnHeiht);
    [selectBtn setImage:[UIImage imageNamed:@"message_album_unselect"] forState:UIControlStateNormal];
    [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(4,10,10,4)];
    
    selectBtn.tag = kTag;
    [selectBtn addTarget:self action:@selector(selectPhotoBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button addSubview:selectBtn];
}

- (void)hideAllSubViews
{
    _firstPhotoBtn.hidden = YES;
    _secondPhotoBtn.hidden = YES;
    _thirdPhotoBtn.hidden = YES;
    _fourPhotoBtn.hidden  = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
